from BaseView import BaseWebView
from BaseControl import WebFindKey
from BaseView import BaseWebViewDelegate, BaseWebViewLeafDelegate, WebViewDelegateIndex, BaseWebViewDelegateBuilder


class WebTableViewCellDelegate(BaseWebViewLeafDelegate):
    def __init__(self, handler, index):
        super(WebTableViewCellDelegate, self).__init__(handler, index)


class WebTableViewRowDelegate(BaseWebViewDelegate):
    def __init__(self, handler, index, childBuilder):
        super(WebTableViewRowDelegate, self).__init__(handler, index, childBuilder)

    def _childHandler(self):
        return WebFindKey.XPATH, "td"

    def isSelected(self):
        return self.get_attribute('class') == 'selected'


class WebTableViewHeader(WebTableViewRowDelegate):
    def __init__(self, handler, index, childBuilder, headerHandler):
        super(WebTableViewHeader, self).__init__(handler, index, childBuilder)
        self._headerHandler = headerHandler

    def _childHandler(self):
        return self._headerHandler


class WebTableViewRootDelegate(BaseWebViewDelegate):
    def __init__(self, handler, index, childBuilder, headerHandler):
        super(WebTableViewRootDelegate, self).__init__(handler, index, childBuilder)
        self._headerHandler = headerHandler

    def header(self):
        return WebTableViewHeader(self.waitChildControl(WebFindKey.XPATH, 'thead/tr'),
                                  WebViewDelegateIndex(self, 0),
                                  self._childrenBuilder,
                                  self._headerHandler)

    def _childHandler(self):
        return WebFindKey.XPATH, "tbody/tr"


class WebTableViewDelegateBuilder(BaseWebViewDelegateBuilder):
    def __init__(self, headerHandler=(WebFindKey.XPATH, "th")):
        super(WebTableViewDelegateBuilder, self).__init__(WebTableViewRootDelegate)
        self._headerHandler = headerHandler

    def build(self, controlHandler, index=WebViewDelegateIndex()):
        if not index.isValid():
            return WebTableViewRootDelegate(controlHandler, index, self, self._headerHandler)
        elif isinstance(index.parent, WebTableViewRootDelegate):
            return WebTableViewRowDelegate(controlHandler, index, self)
        return WebTableViewCellDelegate(controlHandler, index)


class WebTableView(BaseWebView):
    def __init__(self, handler, delegateBuilder=WebTableViewDelegateBuilder()):
        super(WebTableView, self).__init__(handler, delegateBuilder)

    def header(self):
        return self.rootDelegate().header()

    def rootDelegate(self):
        return self._delegateBuilder.build(self.obj())

    def item(self, row, column):
        return self.row(row).children()[column]

    def columnsCount(self):
        return len(self.header().children())

    def selectRow(self, row):
        self.row(row).setSelection(True)

    def column(self, column):
        columnList = []
        for r in range(0, self.rowsCount()):
            columnList.append(self.row(r).children()[column])
        return columnList

    def getRowAsList(self, row):
        return [child.text for child in self.row(row).children()]
